/**
 * Copyright (c) 2015-2016, Javen Zhou  (javenlife@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.bankLogisticsSys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.kit.HashKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.CallbackIpApi;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MessageApi;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.jfinal.weixin.sdk.api.ShorturlApi;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;

public class WeixinApiController extends ApiController {
	
	public ApiConfig getApiConfig() {
		return WeixinUtil.getApiConfig();
	}
	
	/**
	 * 获取公众号关注用户
	 */
	public void getFollowers()
	{
		ApiResult apiResult = UserApi.getFollows();
		renderText(apiResult.getJson());
	}

	/**
	 * 获取用户信息
	 */
	public void getUserInfo()
	{
		ApiResult apiResult = UserApi.getUserInfo("oCRqYwmnwZpesSxZjgw4YlzYYiPA");
		renderText(apiResult.getJson());
	}

	/**
	 * 发送模板消息
	 */
	public void sendMsg()
	{
		String str = " {\n" +
				"           \"touser\":\"oCRqYwmnwZpesSxZjgw4YlzYYiPA\",\n" +
				"           \"template_id\":\"Doclyl5uP7Aciu-qZ7mJNPtWkbkYnWBWVja26EGbNyk\",\n" +
				"           \"url\":\"http://www.sina.com\",\n" +
				"           \"topcolor\":\"#FF0000\",\n" +
				"           \"data\":{\n" +
				"                   \"first\": {\n" +
				"                       \"value\":\"恭喜你购买成功！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword1\":{\n" +
				"                       \"value\":\"去哪儿网发的酒店红包（1个）\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"keyword2\":{\n" +
				"                       \"value\":\"1元\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   },\n" +
				"                   \"remark\":{\n" +
				"                       \"value\":\"欢迎再次购买！\",\n" +
				"                       \"color\":\"#173177\"\n" +
				"                   }\n" +
				"           }\n" +
				"       }";
		ApiResult apiResult = TemplateMsgApi.send(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取参数二维码
	 */
	public void getQrcode()
	{
		String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
		ApiResult apiResult = QrcodeApi.create(str);
		renderText(apiResult.getJson());

//        String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"123\"}}}";
//        ApiResult apiResult = QrcodeApi.create(str);
//        renderText(apiResult.getJson());
		
	}
	
	public void createTemporary(){
		/**
		 * {"ticket":"gQGq8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2xVTnBzUHJtRzlWaXFZeHJBRzI2AAIEnhWaVgMEIBwAAA==","expire_seconds":7200,"url":"http:\/\/weixin.qq.com\/q\/lUNpsPrmG9ViqYxrAG26"}
		 */
		ApiResult createTemporary = QrcodeApi.createTemporary(7200, 123);
		renderText(createTemporary.getJson());
	}
	
	public void getShowQrcodeUrl(){
		String url = QrcodeApi.getShowQrcodeUrl("gQH-7zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyMDQ2SDFaOXFlbzIxcWpjak5wMXoAAgQTUkpZAwSAOgkA");
		renderText(url);
	}

	/**
	 * 长链接转成短链接
	 */
	public void getShorturl()
	{
		String str = "http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1";
		ApiResult apiResult = ShorturlApi.getShortUrl(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取客服聊天记录
	 */
	public void getRecord()
	{
		String str = "{\n" +
				"    \"endtime\" : 987654321,\n" +
				"    \"pageindex\" : 1,\n" +
				"    \"pagesize\" : 10,\n" +
				"    \"starttime\" : 123456789\n" +
				" }";
		ApiResult apiResult = CustomServiceApi.getRecord(str);
		renderText(apiResult.getJson());
	}

	/**
	 * 获取微信服务器IP地址
	 */
	public void getCallbackIp()
	{
		ApiResult apiResult = CallbackIpApi.getCallbackIp();
		renderText(apiResult.getJson());
	}
	/**
	 * 添加多客服
	 */
	public void addKfAccount(){
		ApiResult apiResult = CustomServiceApi.addKfAccount("javen@gh_8746b7fa293e", "javen", HashKit.md5("javen123"));
		renderText(apiResult.getJson());
	}
	
	//function: send customer service message to user
	public void sendCustomerMsg(){
//		String sendCustomerMsg = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
//		ParaMap pm = ParaMap.create("access_token", AccessTokenApi.getAccessTokenStr()).put("openid", openId).put("lang", "zh_CN");
		ApiResult apiResult = CustomServiceApi.sendText("oCRqYwmnwZpesSxZjgw4YlzYYiPA", "测试发送客服消息");
		renderText(apiResult.getJson());
	}
	
	//function: send customer service message to user
	public void sendProcessMsg(String openId, String msg){
		ApiResult apiResult = CustomServiceApi.sendText(openId, msg);
		renderText(apiResult.getJson());
	}
	
	public void sendAll(){
		ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
		JSONObject obj = new JSONObject();
		JSONObject obj2 = new JSONObject();
		JSONObject obj3 = new JSONObject();
		JSONArray ja = new JSONArray();
		ja.add("oCRqYwnYL9c-9MeVgp8cGl4VwJUY");
		ja.add("oCRqYwmnwZpesSxZjgw4YlzYYiPA");
		obj2.put("is_to_all", false);
		obj2.put("tag_id", 2);
		obj.put("filter", obj2);
//		obj.put("touser", ja);
		obj3.put("content", "nihao");
		obj.put("text", obj3);
		obj.put("msgtype", "text");
		String str = "{\"filter\":{\"is_to_all\":true},\"text\":{\"content\":\"test\"},\"msgtype\":\"text\"}\"";
		
		ApiResult apiResult = MessageApi.send(str);
		renderText(apiResult.getJson());
	}
}

